package controllers

import (
	"LoveU/models"
	"LoveU/utils"
	"strconv"

	"github.com/astaxie/beego"
)

type ActivityController struct {
	beego.Controller
}

//获取最新的活动
func (this *ActivityController) GetActivity() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginActivity(this)
		return
	}
	// userid := mySession.Get("userid")
	activity := models.GetActivity()
	if activity != nil {
		getJSONActivity(this, utils.OK, "success", "", activity)
	}
	getJSONActivity(this, utils.UNKNOWN_ERROR, "未知错误或没有活动", "", nil)

}

//参加活动
func (this *ActivityController) AttendActivity() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginActivity(this)
		return
	}
	userid := mySession.Get("userid")
	aidStr := this.Input().Get("aid")
	aid, err := strconv.ParseInt(aidStr, 10, 64)
	if err != nil {
		getJSONActivity(this, utils.ARGUMENT_DENIED, "参数错误", "", nil)
		return
	}
	status := models.AttendActivity(aid, userid.(int64))
	switch status {
	case utils.ACTIVITY_ATTEND_OK:
		getJSONActivity(this, status, "success", "", nil)
	case utils.ACTIVITY_ATTENDED:
		getJSONActivity(this, status, "您已参加此活动", "", nil)
	case utils.ACTIVITY_NOT_FOUND:
		getJSONActivity(this, status, "不存在该ID对应的活动", "", nil)
	default:
		getJSONActivity(this, status, "其他错误", "", nil)
	}
}

// 取消活动
func (this *ActivityController) CancelActivity() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginActivity(this)
		return
	}
	userid := mySession.Get("userid")
	aidStr := this.Input().Get("aid")
	aid, err := strconv.ParseInt(aidStr, 10, 64)
	if err != nil {
		getJSONActivity(this, utils.ARGUMENT_DENIED, "参数错误", "", nil)
		return
	}
	status := models.CancelActivity(aid, userid.(int64))
	if status == true {
		getJSONActivity(this, utils.OK, "success", "", nil)
		return
	}
	getJSONActivity(this, utils.UNKNOWN_ERROR, "取消活动失败", "", nil)
	return
}

//获取全部活动圈
func (this *ActivityController) GetActivities() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginActivity(this)
		return
	}
	userid := mySession.Get("userid")
	//获取分页
	page_str := this.Input().Get("page")
	if page_str == "" {
		getJSONActivity(this, utils.ARGUMENT_DENIED, "参数错误41", "", nil)
		return
	}
	page, err0 := strconv.ParseInt(page_str, 10, 64)
	pageSize_str := this.Input().Get("pagesize")
	if pageSize_str == "" {
		getJSONActivity(this, utils.ARGUMENT_DENIED, "参数错误47", "", nil)
		return
	}
	pageSize, err1 := strconv.ParseInt(pageSize_str, 10, 64)

	if err0 != nil || err1 != nil {
		getJSONActivity(this, utils.ARGUMENT_DENIED, "参数错误63", "", nil)
		return
	}

	activities, status := models.GetUserActivities(page, pageSize, userid.(int64))
	switch status {
	case utils.UNKNOWN_ERROR:
		getJSONActivity(this, utils.UNKNOWN_ERROR, "系统错误", "", nil)
		return
	case utils.OK:
		getJSONActivity(this, utils.OK, "success", "", activities)
		return
	default:
		getJSONActivity(this, utils.UNKNOWN_ERROR, "系统错误", "", nil)
		return
	}
}

func (this *ActivityController) GetMyActivities() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginActivity(this)
		return
	}
	userid := mySession.Get("userid")

	//获取分页
	page_str := this.Input().Get("page")
	if page_str == "" {
		getJSONActivity(this, utils.ARGUMENT_DENIED, "参数错误41", "", nil)
		return
	}
	page, err0 := strconv.ParseInt(page_str, 10, 64)
	pageSize_str := this.Input().Get("pagesize")
	if pageSize_str == "" {
		getJSONActivity(this, utils.ARGUMENT_DENIED, "参数错误47", "", nil)
		return
	}
	pageSize, err1 := strconv.ParseInt(pageSize_str, 10, 64)

	if err0 != nil || err1 != nil {
		getJSONActivity(this, utils.ARGUMENT_DENIED, "参数错误63", "", nil)
		return
	}

	activities, status := models.GetMyActivities(userid.(int64), page, pageSize)
	switch status {
	case utils.UNKNOWN_ERROR:
		getJSONActivity(this, utils.UNKNOWN_ERROR, "系统错误", "", nil)
		return
	case utils.OK:
		getJSONActivity(this, utils.OK, "success", "", activities)
		return
	default:
		getJSONActivity(this, utils.UNKNOWN_ERROR, "系统错误", "", nil)
		return
	}

}
